//
//  ZSY_DestinationListViewController.m
//  zsy_BreadTrip
//
//  Created by dllo on 15/12/28.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "ZSY_DestinationListViewController.h"
#import "ZSY_NearbyViewController.h"
#import "ZSY_DestinationListTableViewCell.h"
#import "ZSY_DestinationModel.h"
#import "ZSY_DestinaSection.h"
#import "ZSY_DestinationDetailViewController.h"
#import "ZSY_MoreListViewController.h"
#import "ZSY_DestinationScrollView.h"
#import "ZSY_HomeScrollViewDetailViewController.h"


@interface ZSY_DestinationListViewController ()<UITableViewDataSource, UITableViewDelegate, DestinationDelegate, ZSY_DestinationScrollViewDelegate>

@property (nonatomic, retain) NSMutableArray *scrollArray;
@property (nonatomic, retain) ZSY_BaseTableView *destinationTableView;
@property (nonatomic, retain) NSMutableArray *festivalArray;
@property (nonatomic, retain) NSMutableArray *hotArray;
@property (nonatomic, retain) ZSY_BaseLabel *sectionLabel;
@property (nonatomic, retain) NSMutableArray *sectionArray;

@property (nonatomic, retain) NSMutableArray *EuropeArray;
@property (nonatomic, retain) NSMutableArray *HKArray;
@property (nonatomic, retain) NSMutableArray *USArray;
@property (nonatomic, retain) NSMutableArray *AsiaArray;
@property (nonatomic, retain) NSMutableArray *nationalArray;

@property (nonatomic, retain) ZSY_DestinationListTableViewCell *cell;

@property (nonatomic, retain) ZSY_MoreListViewController *europeVC;


@property(nonatomic,assign)NSInteger nextPage;//记录下一页
@property(nonatomic,assign)BOOL isUpLoading;//标记上拉操作还是下拉操作，yes为上拉


@end

@implementation ZSY_DestinationListViewController

- (void)dealloc {
    [_europeVC release];
    [_cell release];
    [_sectionArray release];
    [_sectionLabel release];
    [_EuropeArray release];
    [_HKArray release];
    [_USArray release];
    [_AsiaArray release];
    [_nationalArray release];
    [_hotArray release];
    [_destinationTableView release];
    [_festivalArray release];
    [_scrollArray release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.view.backgroundColor = [UIColor colorWithRed:248 / 255.0 green:243 / 255.0 blue:233 / 255.0 alpha:1];
    // 初始化数组
    self.scrollArray = [NSMutableArray array];
    self.festivalArray = [NSMutableArray array];
    self.sectionArray = [NSMutableArray array];
    self.hotArray = [NSMutableArray array];
    self.EuropeArray = [NSMutableArray array];
    self.HKArray = [NSMutableArray array];
    self.AsiaArray = [NSMutableArray array];
    self.USArray = [NSMutableArray array];
    self.nationalArray = [NSMutableArray array];
    // 设置tableView 的代理
    
    CGRect frame = self.view.frame;
    frame.size.height -= 84;
    self.destinationTableView = [[ZSY_BaseTableView alloc] initWithFrame:frame style:UITableViewStyleGrouped];
    self.destinationTableView.delegate = self;
    self.destinationTableView.dataSource = self;

//    self.destinationTableView.backgroundColor = [UIColor colorWithRed:248 / 255.0 green:243 / 255.0 blue:233 / 255.0 alpha:1];
    [self.view addSubview:_destinationTableView];
    // 取消cell 的分割线
    self.destinationTableView.separatorStyle = NO;

    
    // 注册tableViewCell
    [self.destinationTableView registerClass:[ZSY_DestinationListTableViewCell class] forCellReuseIdentifier:@"cell"];
    // 右上角附近按钮
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"附近" style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonAction:)];
    
    self.isUpLoading = NO;
    
    // 2.集成上拉下拉功能
    [self addHeader];

    [_destinationTableView release];
    
}
// 创建轮播图
- (ZSY_BaseView *)createScrollView {
    ZSY_BaseView *view = [[ZSY_BaseView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 200)];
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 200)];
    scrollView.contentSize = CGSizeMake(WIDTH * 4, 200);
    for (int i = 0; i < 4; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH * i, 0, WIDTH, 200)];
        NSString *string = [self.scrollArray objectAtIndex:i];
        NSURL *url = [NSURL URLWithString:string];
        //        UIImage *image = [UIImage imageNamed:@"2233.jpg"];
        //        imageView.image = image;
        [imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"2233"]];
        
        [scrollView addSubview:imageView];
        [imageView release];
    }
    scrollView.pagingEnabled = YES;
    [view addSubview:scrollView];
    [scrollView release];
    //    [view release];
    return view;

}

#pragma mark --下拉刷新
- (void)addHeader
{
    //    __unsafe_unretained typeof(self) vc = self;
    
    __block ZSY_DestinationListViewController *vc = self;//因为block块里用self会报警告,所以需要将self声明成block变量
    
    // 添加下拉刷新头部控件
    [self.destinationTableView addHeaderWithCallback:^{
        // 进入刷新状态就会回调这个Block
        
        [self.festivalArray removeAllObjects];
        [self.hotArray removeAllObjects];
        [self.EuropeArray removeAllObjects];
        [self.AsiaArray removeAllObjects];
        [self.USArray removeAllObjects];
        [self.nationalArray removeAllObjects];
        [self.HKArray removeAllObjects];
        [self.sectionArray removeAllObjects];
        [self.scrollArray removeAllObjects];
        vc.nextPage = 0;//记录页码的
        vc.isUpLoading = NO;//标记为下拉操作
        [vc getDataList:vc.nextPage];//重新请求数据
        
    }];
    
    NSFileManager *manager1 = [NSFileManager defaultManager];
    
    NSString *filePath = [ZSY_AppTools createFilePathFromDocumentWithFolders:@[@"Destination", @"DestinationViewController", @"All"] fileName:@"destination.txt"];
    
    //    NSLog(@"~~~~~%@", filePath);
    if ([manager1 fileExistsAtPath:filePath]) {
        NSData *data = [NSData dataWithContentsOfFile:filePath];
        NSMutableDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        /**
         * 轮播图banners
         */
        NSArray *bannersArray = [bigDic objectForKey:@"banners"];
        for (NSDictionary *dic in bannersArray) {
            ZSY_DesScrollModel *model = [[ZSY_DesScrollModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.scrollArray addObject:model];
            [model release];
        }
        /**
         *  下方旅游分区
         */
        NSArray *elementsArray = [bigDic objectForKey:@"elements"];
        for (NSDictionary *dataDic in elementsArray) {
            ZSY_DestinaSection *destinaSection = [[ZSY_DestinaSection alloc] init];
            [destinaSection setValuesForKeysWithDictionary:dataDic];
            [self.sectionArray addObject:destinaSection];
            [destinaSection release];
            
            if (((NSNumber *)[dataDic objectForKey:@"index"]).intValue == 0) {
                /**
                 *  节日景点
                 */
                NSArray *dataArray = [dataDic objectForKey:@"data"];
                for (NSDictionary *dic in dataArray) {
                    ZSY_DestinationModel *destination = [[ZSY_DestinationModel alloc] init];
                    [destination setValuesForKeysWithDictionary:dic];
                    [self.festivalArray addObject:destination];
                    [destination release];
                }
            } else if (((NSNumber *)[dataDic objectForKey:@"index"]).intValue == 2) {
                /**
                 *  热门景点
                 */
                NSArray *dataArray = [dataDic objectForKey:@"data"];
                for (NSDictionary *dic in dataArray) {
                    ZSY_DestinationModel *destination = [[ZSY_DestinationModel alloc] init];
                    [destination setValuesForKeysWithDictionary:dic];
                    [self.hotArray addObject:destination];
                    [destination release];
                }
            } else if (((NSNumber *)[dataDic objectForKey:@"index"]).intValue == 3) {
                /**
                 *  欧洲国家
                 */
                NSArray *dataArray = [dataDic objectForKey:@"data"];
                for (NSDictionary *dic in dataArray) {
                    ZSY_DestinationModel *destination = [[ZSY_DestinationModel alloc] init];
                    [destination setValuesForKeysWithDictionary:dic];
                    [self.EuropeArray addObject:destination];
                    [destination release];
                }
                NSLog(@"array = %ld", _EuropeArray.count);
            } else if (((NSNumber *)[dataDic objectForKey:@"index"]).intValue == 4) {
                /**
                 *  港澳台
                 */
                NSArray *dataArray = [dataDic objectForKey:@"data"];
                for (NSDictionary *dic in dataArray) {
                    ZSY_DestinationModel *destination = [[ZSY_DestinationModel alloc] init];
                    [destination setValuesForKeysWithDictionary:dic];
                    [self.HKArray addObject:destination];
                    [destination release];
                }
            } else if (((NSNumber *)[dataDic objectForKey:@"index"]).intValue == 5) {
                /**
                 *  美洲国家
                 */
                NSArray *dataArray = [dataDic objectForKey:@"data"];
                for (NSDictionary *dic in dataArray) {
                    ZSY_DestinationModel *destination = [[ZSY_DestinationModel alloc] init];
                    [destination setValuesForKeysWithDictionary:dic];
                    [self.USArray addObject:destination];
                    [destination release];
                }
            } else if (((NSNumber *)[dataDic objectForKey:@"index"]).intValue == 6) {
                /**
                 *  亚洲国家
                 */
                NSArray *dataArray = [dataDic objectForKey:@"data"];
                for (NSDictionary *dic in dataArray) {
                    ZSY_DestinationModel *destination = [[ZSY_DestinationModel alloc] init];
                    [destination setValuesForKeysWithDictionary:dic];
                    [self.AsiaArray addObject:destination];
                    [destination release];
                }
            } else if (((NSNumber *)[dataDic objectForKey:@"index"]).intValue == 8) {
                /**
                 *  国内
                 */
                NSArray *dataArray = [dataDic objectForKey:@"data"];
                for (NSDictionary *dic in dataArray) {
                    ZSY_DestinationModel *destination = [[ZSY_DestinationModel alloc] init];
                    [destination setValuesForKeysWithDictionary:dic];
                    [self.nationalArray addObject:destination];
                    [destination release];
                }
            }
        }
        
        ZSY_DestinationScrollView *scrollView = [[ZSY_DestinationScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 200)];
        // 设置tableView的头部视图
        scrollView.myDelegate = self;
        scrollView.allArray = self.scrollArray;
        
        self.destinationTableView.tableHeaderView = scrollView;
        [self.destinationTableView headerEndRefreshing];

        [self.destinationTableView reloadData];
        
    }
    
    
#pragma mark 自动刷新(一进入程序就下拉刷新)
    else{
        
        [self.destinationTableView headerBeginRefreshing];
    }
    
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    self.cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    self.cell.delegate = self;
    if (indexPath.section == 0) {
        
        if (_festivalArray.count != 0) {
            
            _cell.allArray = _festivalArray;
        }
    } else if (indexPath.section == 1) {
        if (_hotArray.count != 0) {
            _cell.allArray = _hotArray;
        }
    } else if (indexPath.section == 2) {
        if (_EuropeArray.count != 0) {
            _cell.allArray = _EuropeArray;
            NSLog(@"%ld", _EuropeArray.count);
        }
    } else if (indexPath.section == 3) {
        if (_HKArray.count != 0) {
            _cell.allArray = _HKArray;
        }
    } else if (indexPath.section == 4) {
        if (_USArray.count != 0) {
            _cell.allArray = _USArray;
        }
    } else if (indexPath.section == 5) {
        if (_AsiaArray.count != 0) {
            _cell.allArray = _AsiaArray;
        }
    } else {
        if (_nationalArray.count != 0) {
            _cell.allArray = _nationalArray;
        }
    }
    return _cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return _sectionArray.count;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 4) {
        return 190;
    }
    return 380;
}

// 实现协议方法
- (void)pushToDetailView:(NSString *)url {
    ZSY_DestinationDetailViewController *detailVC = [[ZSY_DestinationDetailViewController alloc] init];

    detailVC.url = url;
    [self.navigationController pushViewController:detailVC animated:YES];
}

// 设置section的内容, 样式
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    ZSY_BaseView *sectionView = [[[ZSY_BaseView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 40)] autorelease];
//    sectionView.backgroundColor = [UIColor colorWithRed:248 / 255.0 green:243 / 255.0 blue:233 / 255.0 alpha:1];
    ZSY_BaseView *leftView = [[ZSY_BaseView alloc] initWithFrame:CGRectMake(10, 15, 8, 20)];
    leftView.backgroundColor = [UIColor cyanColor];
    leftView.layer.cornerRadius = 3;
    leftView.layer.masksToBounds = YES;
    [sectionView addSubview:leftView];
    [leftView release];
    
    self.sectionLabel = [[ZSY_BaseLabel alloc] initWithFrame:CGRectMake(30, 15, 100, 20)];
    ZSY_DestinaSection *model = [_sectionArray objectAtIndex:section];
    self.sectionLabel.text = model.title;
    
    
    [self.sectionLabel sizeToFit];
    self.sectionLabel.font = [UIFont systemFontOfSize:15];
    [sectionView addSubview:_sectionLabel];
    [_sectionLabel release];
    if (model.more == true) {
        
        ZSY_BaseLabel *rightLabel = [[ZSY_BaseLabel alloc] initWithFrame:CGRectMake(WIDTH - 50, 15, 25, 20)];
        rightLabel.text = @"全部";
        rightLabel.textColor = [UIColor lightGrayColor];
        rightLabel.font = [UIFont systemFontOfSize:12];
        [sectionView addSubview:rightLabel];
        [rightLabel release];
        
        ZSY_BaseImageView *rightImageView = [[ZSY_BaseImageView alloc] initWithFrame:CGRectMake(WIDTH - 25, 15, 15, 20)];
        //    rightImageView.backgroundColor = [UIColor blackColor];
        rightImageView.image = [UIImage imageNamed:@"section_icon"];
        [sectionView addSubview:rightImageView];
        [rightImageView release];
        
        ZSY_BaseButton *rightButton = [ZSY_BaseButton buttonWithType:UIButtonTypeCustom];
        rightButton.frame = CGRectMake(WIDTH - 50, 15, 40, 20);
        rightButton.backgroundColor = [UIColor clearColor];
        [rightButton addTarget:self action:@selector(rightAllButton:) forControlEvents:UIControlEventTouchUpInside];
        self.europeVC = [[[ZSY_MoreListViewController alloc] init] autorelease];
        self.europeVC.index = model.index;
        [sectionView addSubview:rightButton];
        
    }
    return sectionView;
}

- (void)rightAllButton:(ZSY_BaseButton *)button {
//    self.europeVC = [[ZSY_AllEuropeViewController alloc] init];
    
    
    [self.navigationController pushViewController:_europeVC animated:YES];
    NSLog(@"正在点击全部按钮");
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 40;
}


// 右上角附近按钮实现
- (void)rightButtonAction:(UIBarButtonItem *)button {
    ZSY_NearbyViewController *nearbyVC = [[ZSY_NearbyViewController alloc] init];
    [self.navigationController pushViewController:nearbyVC animated:YES];
    [nearbyVC release];
}

- (void)method:(NSString *)html_url {
    ZSY_HomeScrollViewDetailViewController *vc = [[ZSY_HomeScrollViewDetailViewController alloc] init];
    vc.html_url = html_url;
    [self.navigationController pushViewController:vc animated:YES];
    [vc release];
}


#pragma 用AFN请求数据
- (void)getDataList:(NSInteger)next {
    // 请求数据
    [ZSY_AppTools creatingDataTask:@"http://api.breadtrip.com/destination/v3/" parameters:nil Cookie:nil User_Agent:nil successAFNBlock:^(id resuposeObject) {
        NSDictionary *bigDic = resuposeObject;
        /**
         * 轮播图banners
         */
        NSArray *bannersArray = [bigDic objectForKey:@"banners"];
        for (NSDictionary *dic in bannersArray) {
            ZSY_DesScrollModel *model = [[ZSY_DesScrollModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.scrollArray addObject:model];
            [model release];
        }
        /**
         *  下方旅游分区
         */
        NSArray *elementsArray = [bigDic objectForKey:@"elements"];
        for (NSDictionary *dataDic in elementsArray) {
            ZSY_DestinaSection *destinaSection = [[ZSY_DestinaSection alloc] init];
            [destinaSection setValuesForKeysWithDictionary:dataDic];
            [self.sectionArray addObject:destinaSection];
            [destinaSection release];
            
            if (((NSNumber *)[dataDic objectForKey:@"index"]).intValue == 0) {
                /**
                 *  节日景点
                 */
                NSArray *dataArray = [dataDic objectForKey:@"data"];
                for (NSDictionary *dic in dataArray) {
                    ZSY_DestinationModel *destination = [[ZSY_DestinationModel alloc] init];
                    [destination setValuesForKeysWithDictionary:dic];
                    [self.festivalArray addObject:destination];
                    [destination release];
                }
            } else if (((NSNumber *)[dataDic objectForKey:@"index"]).intValue == 2) {
                /**
                 *  热门景点
                 */
                NSArray *dataArray = [dataDic objectForKey:@"data"];
                for (NSDictionary *dic in dataArray) {
                    ZSY_DestinationModel *destination = [[ZSY_DestinationModel alloc] init];
                    [destination setValuesForKeysWithDictionary:dic];
                    [self.hotArray addObject:destination];
                    [destination release];
                }
            } else if (((NSNumber *)[dataDic objectForKey:@"index"]).intValue == 3) {
                /**
                 *  欧洲国家
                 */
                NSArray *dataArray = [dataDic objectForKey:@"data"];
                for (NSDictionary *dic in dataArray) {
                    ZSY_DestinationModel *destination = [[ZSY_DestinationModel alloc] init];
                    [destination setValuesForKeysWithDictionary:dic];
                    [self.EuropeArray addObject:destination];
                    [destination release];
                }
                NSLog(@"array = %ld", _EuropeArray.count);
            } else if (((NSNumber *)[dataDic objectForKey:@"index"]).intValue == 4) {
                /**
                 *  港澳台
                 */
                NSArray *dataArray = [dataDic objectForKey:@"data"];
                for (NSDictionary *dic in dataArray) {
                    ZSY_DestinationModel *destination = [[ZSY_DestinationModel alloc] init];
                    [destination setValuesForKeysWithDictionary:dic];
                    [self.HKArray addObject:destination];
                    [destination release];
                }
            } else if (((NSNumber *)[dataDic objectForKey:@"index"]).intValue == 5) {
                /**
                 *  美洲国家
                 */
                NSArray *dataArray = [dataDic objectForKey:@"data"];
                for (NSDictionary *dic in dataArray) {
                    ZSY_DestinationModel *destination = [[ZSY_DestinationModel alloc] init];
                    [destination setValuesForKeysWithDictionary:dic];
                    [self.USArray addObject:destination];
                    [destination release];
                }
            } else if (((NSNumber *)[dataDic objectForKey:@"index"]).intValue == 6) {
                /**
                 *  亚洲国家
                 */
                NSArray *dataArray = [dataDic objectForKey:@"data"];
                for (NSDictionary *dic in dataArray) {
                    ZSY_DestinationModel *destination = [[ZSY_DestinationModel alloc] init];
                    [destination setValuesForKeysWithDictionary:dic];
                    [self.AsiaArray addObject:destination];
                    [destination release];
                }
            } else if (((NSNumber *)[dataDic objectForKey:@"index"]).intValue == 8) {
                /**
                 *  国内
                 */
                NSArray *dataArray = [dataDic objectForKey:@"data"];
                for (NSDictionary *dic in dataArray) {
                    ZSY_DestinationModel *destination = [[ZSY_DestinationModel alloc] init];
                    [destination setValuesForKeysWithDictionary:dic];
                    [self.nationalArray addObject:destination];
                    [destination release];
                }
            }
        }
        
        ZSY_DestinationScrollView *scrollView = [[ZSY_DestinationScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 200)];
        // 设置tableView的头部视图
        scrollView.myDelegate = self;
        scrollView.allArray = self.scrollArray;

        self.destinationTableView.tableHeaderView = scrollView;
        [self.destinationTableView headerEndRefreshing];

        [self.destinationTableView reloadData];
        
        NSData *data = [NSJSONSerialization dataWithJSONObject:resuposeObject options:NSJSONWritingPrettyPrinted error:nil];
        NSString *filePath = [ZSY_AppTools createFilePathFromDocumentWithFolders:@[@"Destination", @"DestinationViewController", @"All"] fileName:@"destination.txt"];
        [ZSY_AppTools saveDataOnLocal:data localPath:filePath];
    }];
    

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
